package com.fang.guilimall.admin.api.customize.config;

import com.fang.guilimall.admin.api.customize.service.JwtTokenService;
import com.fang.gulimall.admin.infrastructure.user.app.AppLoginUser;
import io.jsonwebtoken.Claims;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

/**
 * @ClassName JwtAuthenticationFilter
 * @Description TODO
 * @Author fangge
 * @Date 2024/6/13 23:22
 */
//@Component
//@Slf4j
//@RequiredArgsConstructor
public class JwtAuthenticationFilter extends OncePerRequestFilter {

    @Autowired
    private JwtTokenService jwtTokenService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        String tokenFromRequest = jwtTokenService.getTokenFromRequest(request);
//
//        if (tokenFromRequest != null) {
//            Claims claims = jwtTokenService.parseToken(tokenFromRequest);
//            String token = (String) claims.get("token");
//            // 根据token去查缓存里面 有没有对应的App用户
//            // 没有的话  再去数据库中查询
//            if (token != null && token.equals("user1")) {
//                AppLoginUser loginUser = new AppLoginUser(23232323L, false, "dasdsadsds");
//                loginUser.grantAppPermission("annie");
//                UsernamePasswordAuthenticationToken suer1 = new UsernamePasswordAuthenticationToken(loginUser, null,
//                        loginUser.getAuthorities());
//                SecurityContextHolder.getContext().setAuthentication(suer1);
//            }
//        }
//
//        filterChain.doFilter(request, response);
    }
}

